//
//  HRSearchBar.swift
//  microBlog
//
//  Created by HR_W on 16/5/19.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

//IBDesgainable:实时显示属性的设置(必须是自定义的视图)

class HRSearchBar: UIView {

    @IBOutlet weak var rightCons: NSLayoutConstraint!

    
    @IBOutlet weak var cancleBtn: UIButton!
    
    @IBOutlet weak var textField: UITextField!
   
    //提供加载nib文件的类方法
    //在func 前加"class"就是类方法
    
  class func loadSearchBar () -> HRSearchBar {
    
        let searchView = NSBundle.mainBundle().loadNibNamed("HRSearchBar", owner: nil, options: nil).last as! HRSearchBar
        return searchView
    
    }
    //初始化操作
    override func awakeFromNib() {
        //设置宽度 bounds.width中的width是bounds的只读属性
        //要修改大小需要通过size来进行修改
        self.bounds.size.width = ScreenWidth
        //文本输入框有leftView
        textField.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        //设置显示模式
        textField.leftViewMode = .Always
 
        textField.leftView?.contentMode = .Center
        
        //设置frame
        textField.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        //设置圆角
//        textField.layer.cornerRadius = 20
//        
//        textField.layer.masksToBounds = true
//        
        //设置边线的颜色和宽度
//        textField.layer.borderWidth = 2
//        
//        textField.layer.borderColor = UIColor.orangeColor().CGColor
        
    }
   
    @IBAction func textFieldBeginEditing(sender: AnyObject) {
        
        UIView.animateWithDuration(0.25) {
            //修改约束 0.25 系统默认的动画时间
            self.rightCons.constant = self.cancleBtn.bounds.width
            //提前强制刷新视图
            self.layoutIfNeeded()
        }
        
        
        
    }
    @IBAction func cancleBtnClick(sender: AnyObject) {
        
        
         self.rightCons.constant = 0
     UIView.animateWithDuration(0.25) { 
  
       
        
        self.layoutIfNeeded()
        }
        
        
        //结束编制状态 的2种方式
        //textField.resignFirstResponder()
        self.endEditing(true)
        
    }

}
